package z_mxsTest.testTableManager;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
//import org.eclipse.swt.widgets.Text;

import ma.xin.shi.table.TableManager;
//import ma.xin.shi.table.TableManager.EditableTableConfig;

public class testTableManager {
	
    public static void main(String[] args) {
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setText("TableManager Test");
        shell.setSize(600, 400);
        shell.setLayout(new FillLayout());

        // 创建表格
        Table table = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION);
        table.setHeaderVisible(true);
        table.setLinesVisible(true);

        // 设置表格列
        String[] columnNames = {"ID", "Name", "Status", "Type"};
        for (String name : columnNames) {
            TableColumn column = new TableColumn(table, SWT.NONE);
            column.setText(name);
            column.setWidth(150);
        }

        // 添加测试数据
        String[][] data = {
            {"1", "Item 1", "Active", "Type A"},
            {"2", "Item 2", "Inactive", "Type B"},
            {"3", "Item 3", "Active", "Type C"}
        };

        for (String[] row : data) {
            TableItem item = new TableItem(table, SWT.NONE);
            item.setText(row);
        }

        // 设置表格为可编辑，使用匿名内部类实现配置接口
        TableManager.setupEditableTable(table, new TableManager.EditableTableConfig() {
            @Override
            public boolean isComboColumn(int columnIndex) {
                // 假设第2列(Status)和第3列(Type)使用下拉框
                return columnIndex == 2 || columnIndex == 3;
            }

            @Override
            public String[] getComboItems(int columnIndex) {
                if (columnIndex == 2) {
                    return new String[]{"Active", "Inactive", "Pending"};
                } else if (columnIndex == 3) {
                    return new String[]{"Type A", "Type B", "Type C", "Type D"};
                }
                return null;
            }
        });

        // 打印表格和列宽信息
        System.out.println("表格初始化后：");
        TableManager.printTableAndColumnWidths(table);

        // 显示窗口
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }

        // 窗口关闭后再次打印表格宽度信息
        System.out.println("\n窗口关闭后：");
        TableManager.printTableTotalWidth(table);

        display.dispose();
    }
}